<template>
  <el-dialog
    :title="'选择地块'"
    :visible.sync="dialogVisible"
    width="13.4rem"
    :before-close="dialogBeforeClose"
    append-to-body
    class="dialog"
  >
    <div class="searchBox">
        <el-select
            v-model="params.plantingVariety"
            @change="getLandinfoList()"
            popper-class="selectInp"
            :popper-append-to-body="false"
            class="searchInp"
            placeholder="请选择种植种类"
            clearable
        >
            <el-option
                v-for="item in plantingVarietys"
                :key="item.id"
                :label="item.name"
                :value="item.code"
            >
            </el-option>
        </el-select>
        <el-select
            v-model="params.landType"
            @change="getLandinfoList()"
            class="searchInp"
            clearable
            placeholder="请选择地块类型"
        >
            <el-option
                v-for="item in landTypes"
                :key="item.id"
                :label="item.name"
                :value="item.code"
            >
            </el-option>
        </el-select>
        <el-select
            v-model="params.state"
            @change="getLandinfoList()"
            class="searchInp"
            placeholder="请选择地块状态"
            clearable
        >
            <el-option
                v-for="item in landStates"
                :key="item.value"
                :label="item.value"
                :value="item.label"
            >
            </el-option>
        </el-select>
    </div>
    <div class="tableAuto">
      <div class="tableList">
        <div class="tableBox" @click="hasIds(item)" v-for="item in landinfoList" :key="item.landId" :class="landIds.includes(item.landId) && 'checkBox'">
            <div class="tableTitle">{{item.landName}}</div>
            <div class="tableNum">{{item.landArea}}亩</div>
            <div class="tableContent">
                {{item.plantingVariety}} <span>（{{item.plantingTime}}种植）</span>
            </div>
        </div>
      </div>
    </div>
    <div class="flexBtn">
        <el-checkbox-group @change="selectAll()" v-model="checkList">
            <el-checkbox label="全选"></el-checkbox>
        </el-checkbox-group>
        <el-button
          class="editBtn"
          type="primary"
          @click="selectIds()"
          >确定</el-button
        >
    </div>
  </el-dialog>
</template>

<script>
import { getSysSonDictListApi } from '@/api/dict'
import { getLandinfoListApi } from '@/api/baseInfo'

// import farmStockDialog from './components/farmStockDialog'
export default {
  name: 'AgriLandList',
  components: {

  },

  data () {
    return {
      params: {
        baseId: 1,
        current: 1,
        size: 6,
        landType: '',
        plantingVariety: '',
        state: '',
        landName: ''
      },
      tableData: null,
      plantingVarietys: null,
      landTypes: null,
      landStates: [
        { value: '空闲中', label: 0 },
        { value: '种植中', label: 1 }
      ],
      valueArea: '',
      total: null,
      currentPage: 1,
      dialogVisible: false,
      landinfoList: [],
      checkList: [],
      landIds: []
    }
  },
  computed: {
    landIdName () {
      const idName = []
      this.landinfoList.map(item => {
        return this.landIds.includes(item.landId) && idName.push(item.landName)
      })
      return idName
    }
  },
  created () {
    this.baseId = this.$store.getters.userInfo.baseId
    this.getSysSonDictList()
    this.getLandinfoList()
  },

  methods: {
    // 全选
    selectAll () {
      this.checkList.length ? this.landIds = this.landinfoList.map(item => { return item.landId }) : this.landIds = []
    },
    // 获取已有的id
    getIdHas (landIds) {
      this.landIds = landIds ? landIds.split(',') : []
      this.checkList = []
      this.dialogVisible = true
    },
    // 判断id 是否存在
    hasIds (item) {
      !this.landIds.includes(item.landId)
        ? this.landIds.push(item.landId)
        : this.landIds = this.landIds.filter(row => {
          return row !== item.landId
        })
    },
    selectIds () {
      this.dialogBeforeClose()
      this.$emit('getLandIds', this.landIds.toString(), this.landIdName)
    },
    // 字典
    async getSysSonDictList () {
      // 种植种类
      const res1 = await getSysSonDictListApi('ZZZL')
      // console.log(res1.data)
      this.plantingVarietys = res1.data
      // 地块类型
      const res2 = await getSysSonDictListApi('DKLX')
      // console.log(res2.data)
      this.landTypes = res2.data
    },
    dialogBeforeClose () {
      this.dialogVisible = false
      this.addForm = this.$options.data().addForm
    },
    // 地块列表
    getLandinfoList () {
      getLandinfoListApi({ ...this.params, baseId: this.baseId }).then(res => {
        this.landinfoList = res.data
      })
    }
  }
}
</script>
<style scoped lang="scss">
.searchBox {
    font-weight: 600;
    margin-bottom: 1.5625vw;
    background: #ffffff;
    .searchInp {
        width: 12vw;
        height: 0.75rem * 0.779;
        margin-right: 1vw;
      }
      .searchBtn {
        background-color: #6adfae;
        border: #80efb1;
        font-weight: 600;
        width: 5.8333vw;
        height: 3.125vw * 0.779;
      }
      .searchBtn:hover {
        background-color: #80efb1;
      }
}
.back {
  position: absolute;
  top: 5%;
  left: 5%;
}
.name {
  display: inline-block;
  ::v-deep .el-form-item__content {
    display: flex;
  }
  .addBtn {
    margin-left: .5208vw;
  }
}
.tableAuto {
  overflow-y: auto;
  height: 5rem;
}
.tableList {
    display: flex;
    flex-wrap: wrap;
    .tableBox {
        width: 2.6rem;
        padding: 0.15rem 0.2rem 0.15rem 0.2rem;
        background: #F6F8F9;
        border-radius: 0.2rem;
        border: 1px solid #F6F8F9;
        margin-right: 0.2rem;
        margin-bottom: 0.2rem;
        > div {
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }
        &:nth-child(4n) {
            margin-right: 0
        }
        .tableTitle {
            font-size: 0.23rem;
            font-weight: bold;
        }
        .tableNum {
            color: #999999;
            margin: 0.15rem 0
        }
        .tableContent {
            background: #D1F0DF;
            color: #19B560;
            text-align: center;
            border-radius: 1rem;
            padding: 0.03rem 0;
            span {
                color: #999
            }
        }
    }
    .checkBox {
        border: 1px solid #19B560
    }
}
::-webkit-scrollbar {
    width: 0;
}
::-webkit-scrollbar-track {
    // background-color: red;
    background-color: #fff;
    border-radius: 0.15vw;
}
::-webkit-scrollbar-track-piece{
    background-color: #fff;
    border-radius: 0.15vw;
}
::-webkit-scrollbar-thumb{
    background-color: #fff;
    border-radius: 0.15vw;
}
.flexBtn {
    padding-top: 0.2rem;
    border-top: 1px solid #CCCCCC;
    display: flex;
    justify-content: space-between;
    align-items: center;
}
</style>
<style>
/* .el-dialog__header {
  background: #002a52;
  text-align: center;
} */
.el-dialog__title {
  border-left: 3px solid #19b560;
  padding: 0 0.5208vw;
  font-weight: 500;
}
</style>
